package com.tsing.basic.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tsing.basic.entity.Permission;
import com.tsing.basic.vo.req.PermissionPageReqVo;
import com.tsing.basic.vo.resp.PermissionRespVo;
import com.tsing.common.vo.RolePerVo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
public interface PermissionMapper extends BaseMapper<Permission> {

    @Select("select distinct rp.role_id role,p.url permission\n" +
            "from t_permission p\n" +
            "inner join t_role_permission rp on p.id=rp.permission_id\n" +
            "inner join t_role r on rp.role_id=r.id")
    List<RolePerVo> selectAllpermission();

    @Select("<script>" +
            "select 1 from t_role_permission rp\n" +
            "inner join t_role r on rp.role_id=r.id\n" +
            "where rp.permission_id in\n" +
            "<foreach collection='ids' item='item' open='(' close=')' separator=',' >\n" +
            "#{item}\n" +
            "</foreach>\n" +
            "limit 1" +
            "</script>")
    String haveRelevance(@Param("ids") List<String> ids);

    @Select("<script>" +
            "select p.id,p.url,p.description remark\n" +
            "from t_permission p\n" +
            "<where>\n" +
            "<if test=\"reqVo.url!=null and reqVo.url!=''\">and p.url like concat('%',#{reqVo.url},'%')</if>\n" +
            "<if test=\"reqVo.remark!=null and reqVo.remark!=''\">and p.description like concat('%',#{reqVo.remark},'%')</if>\n" +
            "</where>\n" +
            "</script>")
    List<PermissionRespVo> permissionPage(PermissionPageReqVo reqVo, Page<Permission> page);

}
